﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code;
using Manager.Common;
using Manager.ViewModels;
using System.Linq;
using Manager.App_Code.REPORTS;

namespace Manager.FormAdd_Edit
{
    public partial class frm_CheckOutMain : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        PhieuDangKyPhong phieuDangKy = null;
        List<PhieuDangKyPhong> lstPhieuDangKy = null;
        List<DangKyPhongViewModel> lstPDKP = null;//cua form checkout
        List<DangKyPhongViewModel> lstPDKPEdit = null;//cua form checkout detail        
        List<DichVuViewModel> lstDichVuViewModel = null;
        CheckOut checkOut = null;
        public string MaPhong { get; set; }
        double tongTienVND;

        public frm_CheckOutMain()
        {
            MaPhong = "";
            InitializeComponent();
        }
        
        public frm_CheckOutMain(string _MaPhong)
        {
            InitializeComponent();
             MaPhong = _MaPhong;
        }

        public frm_CheckOutMain(PhieuDangKyPhong _phieuDangKy)
        {
            InitializeComponent();
            phieuDangKy = _phieuDangKy;
        }

        public frm_CheckOutMain(List<PhieuDangKyPhong> _lstPhieuDK)
        {
            InitializeComponent();
            lstPhieuDangKy = _lstPhieuDK;
        }              

        private void frm_CheckOutMain_Load(object sender, EventArgs e)
        {
            try
            {
                btnInHoaDon.Enabled = false;
                dateCheckout.Text = DateTime.Now.ToShortDateString();
                tongTienVND = 0;
                txtGioRa.Text = DateTime.Now.ToString("HH:mm");
                cmbTienTe.DataSource = db.TienTes.ToList();
                cmbTienTe.DisplayMember = "TenDonVi";
                cmbTienTe.ValueMember = "MaDonVi";
                cmbTienTe.SelectedValue = "VND";

                if (phieuDangKy != null)
                {
                    //phieuDangKy = db.PhieuDangKyPhongs.SingleOrDefault(t => t.MaPhong == MaPhong && t.TinhTrang == false);
                    LoadCheckOut(phieuDangKy);
                }
                else if (lstPhieuDangKy != null)
                {
                    LoadListCheckOut(lstPhieuDangKy);
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("frm_CheckOutMain:frm_CheckOutMain_Load " + ex.Message + "\n" + ex.StackTrace);
                MessageBox.Show(ex.Message);
            }
        }

        public void LoadCheckOut(PhieuDangKyPhong _phieuDangKy)
        {
            txtMaPhong.Text = _phieuDangKy.ChiTietPhieuDangKy.MaPhong;

            lstPDKP = new List<DangKyPhongViewModel>();
            var vmDangKyPhong = new DangKyPhongViewModel();
            txtGioVao.Text = phieuDangKy.GioVao + ":" + phieuDangKy.PhutVao;
            vmDangKyPhong.MaPhieuDat = _phieuDangKy.MaPhieu.ToString();
            vmDangKyPhong.MaPhong = _phieuDangKy.ChiTietPhieuDangKy.MaPhong;
            vmDangKyPhong.TenKhachHang = _phieuDangKy.ChiTietPhieuDangKy.KhachHang.TenKH;
            var phong = db.Phongs.SingleOrDefault(i => i.MaPhong == vmDangKyPhong.MaPhong);
            vmDangKyPhong.TenLoaiPhong = phong.LoaiPhong1.TenLoaiPhong;
            var soNgayO = (DateTime.Now - _phieuDangKy.NgayDen.Value).Days;
            vmDangKyPhong.NgayDen = _phieuDangKy.NgayDen.Value.ToShortDateString();
            vmDangKyPhong.dateNgayDen = _phieuDangKy.NgayDen.Value;
            vmDangKyPhong.NgayDi = _phieuDangKy.NgayDi.Value.ToShortDateString();
            vmDangKyPhong.dateNgayDi = _phieuDangKy.NgayDi.Value;
            vmDangKyPhong.NgayTra = DateTime.Now.ToShortDateString();
            vmDangKyPhong.dateNgayTra = DateTime.Now;
            vmDangKyPhong.strTienCoc = _phieuDangKy.TraTruoc == null ? "0" : CommonFunction.AddSeparator(_phieuDangKy.TraTruoc.Value);
            vmDangKyPhong.TienCoc = _phieuDangKy.TraTruoc == null ? 0 : _phieuDangKy.TraTruoc.Value;
            var giaPhong = _phieuDangKy.ChiTietPhieuDangKy.GiaPhong.Value;
            //var tienCoc = _phieuDangKy.TraTruoc.Value;
            vmDangKyPhong.strGiaPhong = CommonFunction.AddSeparator(giaPhong);
            vmDangKyPhong.GiaPhong = giaPhong;
            vmDangKyPhong.GiaPhong_DonViTienTe = _phieuDangKy.ChiTietPhieuDangKy.MaTienTe;
            vmDangKyPhong.TienCoc_DonViTienTe = _phieuDangKy.TienTe1.MaDonVi;
            var tienTe_GiaPhong = db.TienTes.SingleOrDefault(t => t.MaDonVi == vmDangKyPhong.GiaPhong_DonViTienTe);
            //var tienTe_TienCoc = db.TienTes.SingleOrDefault(t => t.MaDonVi == vmDangKyPhong.TienCoc_DonViTienTe);
            vmDangKyPhong.ThanhTien = giaPhong * soNgayO;
            vmDangKyPhong.strThanhTien = CommonFunction.AddSeparator(vmDangKyPhong.ThanhTien);
            vmDangKyPhong.ThanhTienVND = giaPhong * soNgayO * tienTe_GiaPhong.TyGiaThamKhao.Value;
            vmDangKyPhong.strThanhTienVND = CommonFunction.AddSeparator(vmDangKyPhong.ThanhTienVND);
            lstPDKP.Add(vmDangKyPhong);

            gridListRoom.DataSource = lstPDKP;

            var lstPhuThuDichVu = db.PhuThuDichVus.Where(t => t.MaPhieuDangKy == _phieuDangKy.MaPhieu && t.MaPhong == _phieuDangKy.ChiTietPhieuDangKy.MaPhong).ToList();
            if (lstPhuThuDichVu.Count > 0)
            {
                lstDichVuViewModel = new List<DichVuViewModel>();
                foreach (var phuThuDV in lstPhuThuDichVu)
                {
                    lstDichVuViewModel.Add(new DichVuViewModel
                    {
                        MaDV = phuThuDV.MaDichVu,
                        MaPhieuDangKy = phuThuDV.MaPhieuDangKy,
                        MaPhuThu = phuThuDV.MaPhuThu,
                        TenDichVu = phuThuDV.TenDichVu,
                        DonViTienTe = phuThuDV.MaTienTe,
                        GiaDV = phuThuDV.GiaTien,
                        NgayTao = phuThuDV.NgayTao,
                        SoLuong = phuThuDV.SoLuong.Value,
                        ThanhTien = phuThuDV.SoLuong.Value * phuThuDV.GiaTien.Value,
                        ThanhTienVND = phuThuDV.SoLuong.Value * phuThuDV.GiaTien.Value * phuThuDV.TienTe.TyGiaThamKhao.Value,
                    });
                    tongTienVND += (phuThuDV.ThanhTien.Value * phuThuDV.TienTe.TyGiaThamKhao.Value);
                }
                gridDetailService.DataSource = lstDichVuViewModel;
            }
            foreach (var vm in lstPDKP)
            {
                tongTienVND += vm.ThanhTienVND;
            }
            var tienTe_TienCoc = db.TienTes.SingleOrDefault(t => t.MaDonVi == vmDangKyPhong.TienCoc_DonViTienTe);
            tongTienVND -= (vmDangKyPhong.TienCoc * tienTe_TienCoc.TyGiaThamKhao.Value);
            txtTongTien.Text = CommonFunction.AddSeparator(tongTienVND);
            cmbTienTe.SelectedValue = "VND";
        }

        public void LoadListCheckOut(List<PhieuDangKyPhong> _lstPhieuDangKy)
        {
            txtMaPhong.Enabled = false;
            butChon.Visible = false;

            lstPDKP = new List<DangKyPhongViewModel>();
            foreach (var _phieuDangKy in _lstPhieuDangKy)
            {
                var vmDangKyPhong = new DangKyPhongViewModel();
                txtGioVao.Text = _phieuDangKy.GioVao + ":" + _phieuDangKy.PhutVao;
                vmDangKyPhong.MaPhieuDat = _phieuDangKy.MaPhieu.ToString();
                vmDangKyPhong.MaPhong = _phieuDangKy.ChiTietPhieuDangKy.MaPhong;
                vmDangKyPhong.TenKhachHang = _phieuDangKy.ChiTietPhieuDangKy.KhachHang.TenKH;
                var phong = db.Phongs.SingleOrDefault(i => i.MaPhong == vmDangKyPhong.MaPhong);
                vmDangKyPhong.TenLoaiPhong = phong.LoaiPhong1.TenLoaiPhong;
                var soNgayO = (DateTime.Now - _phieuDangKy.NgayDen.Value).Days;
                vmDangKyPhong.NgayDen = _phieuDangKy.NgayDen.Value.ToShortDateString();
                vmDangKyPhong.dateNgayDen = _phieuDangKy.NgayDen.Value;
                vmDangKyPhong.NgayDi = _phieuDangKy.NgayDi.Value.ToShortDateString();
                vmDangKyPhong.dateNgayDi = _phieuDangKy.NgayDi.Value;
                vmDangKyPhong.NgayTra = DateTime.Now.ToShortDateString();
                vmDangKyPhong.dateNgayTra = DateTime.Now;
                vmDangKyPhong.strTienCoc = _phieuDangKy.TraTruoc == null ? "0" : CommonFunction.AddSeparator(_phieuDangKy.TraTruoc.Value);
                vmDangKyPhong.TienCoc = _phieuDangKy.TraTruoc.Value;
                var giaPhong = _phieuDangKy.ChiTietPhieuDangKy.GiaPhong.Value;
                //var tienCoc = _phieuDangKy.TraTruoc.Value;
                vmDangKyPhong.strGiaPhong = CommonFunction.AddSeparator(giaPhong);
                vmDangKyPhong.GiaPhong = giaPhong;
                vmDangKyPhong.GiaPhong_DonViTienTe = _phieuDangKy.ChiTietPhieuDangKy.MaTienTe;
                vmDangKyPhong.TienCoc_DonViTienTe = _phieuDangKy.TienTe1.MaDonVi;
                var tienTe_GiaPhong = db.TienTes.SingleOrDefault(t => t.MaDonVi == vmDangKyPhong.GiaPhong_DonViTienTe);
                //var tienTe_TienCoc = db.TienTes.SingleOrDefault(t => t.MaDonVi == vmDangKyPhong.TienCoc_DonViTienTe);
                vmDangKyPhong.strThanhTien = CommonFunction.AddSeparator(giaPhong * soNgayO);
                vmDangKyPhong.strThanhTienVND = CommonFunction.AddSeparator(giaPhong * soNgayO * tienTe_GiaPhong.TyGiaThamKhao.Value);
                lstPDKP.Add(vmDangKyPhong);
            }

            gridListRoom.DataSource = lstPDKP;                        
        }
     
        private void butChon_Click(object sender, EventArgs e)
        {
            frm_ListRoomRegistered listr = new frm_ListRoomRegistered();
            listr.FormClosed += new FormClosedEventHandler(listr_FormClosed);
            listr.ShowDialog();
        }

        void listr_FormClosed(object sender, FormClosedEventArgs e)
        {
            var temp = (frm_ListRoomRegistered)sender;
            //txtMaPhieuDK.Text = temp._MaPhieuDangKy;
            txtMaPhong.Text = temp._MaPhong;
            if (txtMaPhong.Text != "")
            {
                phieuDangKy = db.PhieuDangKyPhongs.SingleOrDefault(t => t.ChiTietPhieuDangKy.MaPhong == txtMaPhong.Text && t.TinhTrang == false);
                LoadCheckOut(phieuDangKy);
            }
            //txtTenKhachHang.Text = temp._TenKhachHang;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
       
        private void btnCheckOut_Click(object sender, EventArgs e)
        {            
            var maTienTe = cmbTienTe.SelectedValue.ToString();
            frm_TienThanhToan frm_ThanhToan = new frm_TienThanhToan(txtTongTien.Text, maTienTe);
            frm_ThanhToan.FormClosed += new FormClosedEventHandler(frm_ThanhToan_FormClosed);
            frm_ThanhToan.ShowDialog();
        }

        void frm_ThanhToan_FormClosed(object sender, FormClosedEventArgs e)
        {
            var frm = (frm_TienThanhToan)sender;
            if (frm == null)
                return;
            if (!frm.isClose)            
                CheckOut(frm.tienThanhToanVND, frm.tienThanhToanUSD);
        }

        private void CheckOut(double tienThanhToanVND, double tienThanhToanUSD)
        {
            try
            {
                int index = gridView_ListRoom.FocusedRowHandle;
                var vmDangKyPhong = gridView_ListRoom.GetRow(index) as DangKyPhongViewModel;
                if (vmDangKyPhong != null)
                {
                    #region update checkout
                    checkOut = new CheckOut();
                    phieuDangKy = db.PhieuDangKyPhongs.SingleOrDefault(t => t.ChiTietPhieuDangKy.MaPhong == vmDangKyPhong.MaPhong && t.TinhTrang == false);
                    checkOut.NgayCheckOut = dateCheckout.DateTime;
                    checkOut.NgayTao = DateTime.Now;
                    checkOut.PhieuDangKyPhong = phieuDangKy;
                    checkOut.GioRa = txtGioRa.Text.Split(':')[0];
                    checkOut.PhutRa = txtGioRa.Text.Split(':')[1];
                    checkOut.TongTienTra = CommonFunction.ParseDouble(txtTongTien.Text);
                    checkOut.TienTe = cmbTienTe.SelectedItem as TienTe;
                    checkOut.MaPhong = phieuDangKy.ChiTietPhieuDangKy.MaPhong;
                    checkOut.TienThanhToanVND = tienThanhToanVND;
                    checkOut.TienThanhToanUSD = tienThanhToanUSD;
                    checkOut.GhiChu = "";
                    var lstChiTietCheckOut = new List<ChiTiet_CheckOut>();
                    if (lstPDKPEdit == null)//phieu dang ky phong ko chinh sua gia tien
                    {
                        var soNgayO = (vmDangKyPhong.dateNgayTra - vmDangKyPhong.dateNgayDen).Days;
                        if (soNgayO <= 1)
                        {
                            var chiTiet = new ChiTiet_CheckOut();
                            chiTiet.GiaTien = CommonFunction.ParseDouble(vmDangKyPhong.strThanhTien);
                            chiTiet.MaTienTe = vmDangKyPhong.GiaPhong_DonViTienTe;
                            chiTiet.NgayDen = vmDangKyPhong.dateNgayDen;
                            chiTiet.NgayDi = vmDangKyPhong.dateNgayTra;

                            lstChiTietCheckOut.Add(chiTiet);
                        }
                        else
                        {
                            for (int i = 0; i < soNgayO; i++)
                            {
                                var chiTiet = new ChiTiet_CheckOut();
                                chiTiet.GiaTien = CommonFunction.ParseDouble(vmDangKyPhong.strGiaPhong);
                                chiTiet.MaTienTe = vmDangKyPhong.GiaPhong_DonViTienTe;
                                chiTiet.NgayDen = vmDangKyPhong.dateNgayDen.AddDays(i);
                                chiTiet.NgayDi = vmDangKyPhong.dateNgayDen.AddDays(i + 1);

                                lstChiTietCheckOut.Add(chiTiet);
                            }
                        }
                    }
                    else//co su chinh sua ve gia tien
                    {
                        foreach (var vm in lstPDKPEdit)
                        {
                            var chiTiet = new ChiTiet_CheckOut();
                            chiTiet.GiaTien = CommonFunction.ParseDouble(vm.strGiaPhong);
                            chiTiet.MaTienTe = vm.GiaPhong_DonViTienTe;
                            chiTiet.NgayDen = vm.dateNgayDen;
                            chiTiet.NgayDi = vm.dateNgayDi;

                            lstChiTietCheckOut.Add(chiTiet);
                        }
                    }
                    checkOut.ChiTiet_CheckOuts.AddRange(lstChiTietCheckOut);
                    db.CheckOuts.InsertOnSubmit(checkOut);
                    #endregion
                    #region update phu thu dich vu
                    if (lstDichVuViewModel != null)
                    {
                        var lstPhuThuDichVu = new List<PhuThuDichVu>();
                        foreach (var vmDichVu in lstDichVuViewModel)
                        {
                            var phuThuDichVu = db.PhuThuDichVus.SingleOrDefault(t => t.MaPhuThu == vmDichVu.MaPhuThu);
                            phuThuDichVu.SoLuong = vmDichVu.SoLuong;
                            phuThuDichVu.GiaTien = vmDichVu.GiaDV;
                            phuThuDichVu.ThanhTien = vmDichVu.ThanhTien;
                            lstPhuThuDichVu.Add(phuThuDichVu);
                        }

                    }
                    #endregion
                    #region update lai tinh trang phong
                    var phong = db.Phongs.SingleOrDefault(t => t.MaPhong == vmDangKyPhong.MaPhong);
                    if (phong != null)
                    {
                        phong.TinhTrang = ((int)CommonEnum.TinhTrangPhong.ChuaDon_2).ToString();
                    }
                    #endregion
                    #region Update ngay tra phieu dang ky phong
                    var pdkp = db.PhieuDangKyPhongs.SingleOrDefault(t => t.MaPhieu == phieuDangKy.MaPhieu && t.ChiTietPhieuDangKy.MaPhong == phieuDangKy.ChiTietPhieuDangKy.MaPhong);
                    if (pdkp != null)
                    {
                        pdkp.NgayTra = DateTime.Now;
                        pdkp.TinhTrang = true;
                    }
                    #endregion
                    db.SubmitChanges();
                    if (lstPhieuDangKy == null)
                    {
                        MessageBox.Show("Số tiền phải thanh toán là: " + txtTongTien.Text + " " + cmbTienTe.SelectedValue);
                        btnInHoaDon.Enabled = true;
                        //this.Close();
                    }
                    else
                    {
                        var maphieu = CommonFunction.ParseLong(vmDangKyPhong.MaPhieuDat);
                        var temp = lstPhieuDangKy.SingleOrDefault(t => t.ChiTietPhieuDangKy.MaPhong == vmDangKyPhong.MaPhong && t.MaPhieu == maphieu);
                        if (temp != null)
                            lstPhieuDangKy.Remove(temp);
                        lstPDKP.Remove(vmDangKyPhong);
                        gridListRoom.DataSource = null;
                        gridDetailService.DataSource = null;
                        if (lstPDKP.Count > 0)
                            gridListRoom.DataSource = lstPDKP;
                        MessageBox.Show("Số tiền phải thanh toán là: " + txtTongTien.Text + " " + cmbTienTe.SelectedValue);
                        if (lstPhieuDangKy.Count == 0)
                            btnInHoaDon.Enabled = true;
                    }
                }
                else
                    MessageBox.Show("Vui lòng chọn phòng cần check-out!");
            }
            catch (Exception ex)
            {
                Log.WriteLog("frm_CheckOutMain:btnCheckOut_Click " + ex.Message + "\n" + ex.StackTrace);
                MessageBox.Show(ex.Message);
            }
        }

        private void btnInHoaDon_Click(object sender, EventArgs e)
        {
            //var checkout = db.CheckOuts.SingleOrDefault(t => t.MaPhieuDangKy == 28122013012056 && t.MaPhong == "P101");
            
            var phieuDangKy = db.PhieuDangKyPhongs.SingleOrDefault(t => t.MaPhieu == checkOut.MaPhieuDangKy);
            var gioVao = phieuDangKy.GioVao + ":" + phieuDangKy.PhutVao;
            var gioRa = checkOut.GioRa + ":" + checkOut.PhutRa;
            var tienCoc = phieuDangKy.TraTruoc.Value;
            var maTienTe = phieuDangKy.TienTe;
            var nguoiLapPhieu = phieuDangKy.NguoiDung1;
            RP_HoaDonTraPhong rp = new RP_HoaDonTraPhong(checkOut, tienCoc, maTienTe, gioVao, gioRa, Variables.NguoiDung, nguoiLapPhieu);
            var lstPhuThuDichVu = db.PhuThuDichVus.Where(t => t.MaPhieuDangKy == checkOut.MaPhieuDangKy).ToList();
            var lstDichVuViewModel = new List<DichVuViewModel>();
            var dic = new Dictionary<double?, DichVuViewModel>();
            foreach (var chiTiet in checkOut.ChiTiet_CheckOuts)
            {
                DichVuViewModel vm = null;
                if (dic.ContainsKey(chiTiet.GiaTien))
                    vm = dic[chiTiet.GiaTien];
                if (vm == null)
                {
                    vm = new DichVuViewModel();
                    vm.TuNgay = chiTiet.NgayDen.Value.ToString("dd/MM/yyyy");
                    vm.DenNgay = chiTiet.NgayDi.Value.ToString("dd/MM/yyyy");
                    vm.TenDichVu = "Tiền phòng từ ngày " + vm.TuNgay + " đến ngày " + vm.DenNgay;
                    vm.GiaDV = chiTiet.GiaTien;
                    vm.strGiaDV = CommonFunction.AddSeparator(chiTiet.GiaTien.Value);
                    vm.DonViTienTe = chiTiet.MaTienTe;
                    vm.SoLuong = 1;
                    vm.ThanhTien = vm.GiaDV;
                    vm.ThanhTienVND = vm.GiaDV.Value * chiTiet.TienTe.TyGiaThamKhao.Value;
                    dic.Add(chiTiet.GiaTien, vm);
                }
                else
                {
                    vm.DenNgay = chiTiet.NgayDi.Value.ToString("dd/MM/yyyy");
                    vm.TenDichVu = "Tiền phòng từ ngày " + vm.TuNgay + " đến ngày " + vm.DenNgay;
                    vm.SoLuong++;
                    vm.GiaDV += chiTiet.GiaTien;
                    vm.strGiaDV = CommonFunction.AddSeparator(vm.GiaDV.Value);
                }
            }
            lstDichVuViewModel.AddRange(dic.Values.ToList());
            foreach (var phuThu in lstPhuThuDichVu)
            {
                var vm = new DichVuViewModel();
                vm.MaPhuThu = phuThu.MaPhuThu;
                vm.TenDichVu = phuThu.TenDichVu;
                vm.GiaDV = phuThu.GiaTien;
                vm.strGiaDV = CommonFunction.AddSeparator(phuThu.GiaTien.Value);
                vm.SoLuong = phuThu.SoLuong.Value;
                vm.DonViTienTe = phuThu.MaTienTe;
                vm.ThanhTien = phuThu.GiaTien.Value * phuThu.SoLuong.Value;
                vm.ThanhTienVND = phuThu.GiaTien.Value * phuThu.SoLuong.Value * phuThu.TienTe.TyGiaThamKhao.Value;

                lstDichVuViewModel.Add(vm);
            }
            rp.DataSource = lstDichVuViewModel;
            rp.BindData();
            rp.ShowPreview();
        }

        private void butChuyenPhong_Click(object sender, EventArgs e)
        {
            if (phieuDangKy == null)
            {
                MessageBox.Show("Vui lòng chọn phòng để check out.");
                return;
            }
            frm_DKKhachLe dangky = new frm_DKKhachLe(phieuDangKy);
            dangky.ShowDialog();
        }

        private void cmbTienTe_SelectedIndexChanged(object sender, EventArgs e)
        {
            var tienTe = cmbTienTe.SelectedItem as TienTe;
            var tongTien = Math.Round((tongTienVND / tienTe.TyGiaThamKhao.Value), 2);
            txtTongTien.Text = CommonFunction.AddSeparator(tongTien);
        }

        private void gridListRoom_Click(object sender, EventArgs e)
        {
            int index = gridView_ListRoom.FocusedRowHandle;
            var vmDangKyPhong = gridView_ListRoom.GetRow(index) as DangKyPhongViewModel;
            if (vmDangKyPhong == null)
                return;
            CalculateTongTien(vmDangKyPhong);
        }

        private void gridListRoom_DoubleClick(object sender, EventArgs e)
        {
            int index = gridView_ListRoom.FocusedRowHandle;
            var vmDangKyPhong = gridView_ListRoom.GetRow(index) as DangKyPhongViewModel;
            if (vmDangKyPhong == null)
                return;
            frm_CheckOutDetail frmCheckOutDetail = new frm_CheckOutDetail(vmDangKyPhong, txtGioVao.Text, txtGioRa.Text);
            frmCheckOutDetail.FormClosed += new FormClosedEventHandler(frmCheckOutDetail_FormClosed);
            frmCheckOutDetail.ShowDialog();
        }

        void frmCheckOutDetail_FormClosed(object sender, FormClosedEventArgs e)
        {
            var frm = sender as frm_CheckOutDetail;
            if (!frm.isClose)
            {
                lstPDKPEdit = frm.lstDangKyPhong;
                int index = gridView_ListRoom.FocusedRowHandle;
                var vmDangKyPhong = gridView_ListRoom.GetRow(index) as DangKyPhongViewModel;
                if (vmDangKyPhong != null)
                {
                    vmDangKyPhong.strThanhTienVND = CommonFunction.AddSeparator(frm.TongTienVND);
                    vmDangKyPhong.strThanhTien = CommonFunction.AddSeparator(frm.TongTien);
                }
                CalculateTongTien(vmDangKyPhong);
            }
            
        }

        private void CalculateTongTien(DangKyPhongViewModel vmDangKyPhong)
        {
            tongTienVND = 0;
            tongTienVND += CommonFunction.ParseDouble(vmDangKyPhong.strThanhTienVND);
            if (vmDangKyPhong != null)
            {
                var maphieu = CommonFunction.ParseLong(vmDangKyPhong.MaPhieuDat);
                var lstPhuThuDichVu = db.PhuThuDichVus.Where(t => t.MaPhieuDangKy == maphieu && t.MaPhong == vmDangKyPhong.MaPhong).ToList();
                if (lstPhuThuDichVu.Count > 0)
                {
                    lstDichVuViewModel = new List<DichVuViewModel>();
                    foreach (var phuThuDV in lstPhuThuDichVu)
                    {
                        lstDichVuViewModel.Add(new DichVuViewModel
                        {
                            MaDV = phuThuDV.MaDichVu,
                            MaPhieuDangKy = phuThuDV.MaPhieuDangKy,
                            MaPhuThu = phuThuDV.MaPhuThu,
                            TenDichVu = phuThuDV.TenDichVu,
                            DonViTienTe = phuThuDV.MaTienTe,
                            GiaDV = phuThuDV.GiaTien,
                            NgayTao = phuThuDV.NgayTao,
                            SoLuong = phuThuDV.SoLuong.Value,
                            ThanhTien = phuThuDV.SoLuong.Value * phuThuDV.GiaTien.Value,
                            ThanhTienVND = phuThuDV.SoLuong.Value * phuThuDV.GiaTien.Value * phuThuDV.TienTe.TyGiaThamKhao.Value,
                        });
                        tongTienVND += (phuThuDV.ThanhTien.Value * phuThuDV.TienTe.TyGiaThamKhao.Value);
                    }
                    gridDetailService.DataSource = null;
                    gridDetailService.DataSource = lstDichVuViewModel;
                }
                var tienTe_TienCoc = db.TienTes.SingleOrDefault(t => t.MaDonVi == vmDangKyPhong.TienCoc_DonViTienTe);
                tongTienVND -= (vmDangKyPhong.TienCoc * tienTe_TienCoc.TyGiaThamKhao.Value);
                txtTongTien.Text = CommonFunction.AddSeparator(tongTienVND);
                cmbTienTe.SelectedValue = "VND";
            }
        }

        private void gridView_DetailService_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {

        }

        private void gridDetailService_DoubleClick(object sender, EventArgs e)
        {
            int index = gridView_DetailService.FocusedRowHandle;
            var item = gridView_DetailService.GetRow(index) as DichVuViewModel;
            if (item == null)
                return;
            frm_HieuChinhDichVu frm_hieuchinhdv = new frm_HieuChinhDichVu(item);
            frm_hieuchinhdv.FormClosed += new FormClosedEventHandler(frm_hieuchinhdv_FormClosed);
            frm_hieuchinhdv.ShowDialog();
        }

        void frm_hieuchinhdv_FormClosed(object sender, FormClosedEventArgs e)
        {
            var frm = sender as frm_HieuChinhDichVu;
            if (frm != null)
            {
                int index = gridView_ListRoom.FocusedRowHandle;
                var item = gridView_ListRoom.GetRow(index) as DangKyPhongViewModel;
                var tienTe1 = db.TienTes.SingleOrDefault(t => t.MaDonVi == item.TienCoc_DonViTienTe);
                tongTienVND = 0;
                tongTienVND += CommonFunction.ParseDouble(item.strThanhTienVND);                
                foreach (var vmDichVu in lstDichVuViewModel)
                {
                    if (vmDichVu.MaDV == frm.vmDichVu.MaDV)
                    {
                        vmDichVu.TenDichVu = frm.vmDichVu.TenDichVu;
                        vmDichVu.SoLuong = frm.vmDichVu.SoLuong;
                        vmDichVu.GiaDV = frm.vmDichVu.GiaDV;
                        vmDichVu.ThanhTien = frm.vmDichVu.ThanhTien;
                        vmDichVu.ThanhTienVND = frm.vmDichVu.ThanhTienVND;
                    }
                    tongTienVND += vmDichVu.ThanhTienVND;
                }

                tongTienVND -= (CommonFunction.ParseDouble(item.strTienCoc) * tienTe1.TyGiaThamKhao.Value);
                txtTongTien.Text = CommonFunction.AddSeparator(tongTienVND);
                cmbTienTe.SelectedValue = "VND";
            }
            gridDetailService.DataSource = null;
            gridDetailService.DataSource = lstDichVuViewModel;            
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có thật sự muốn xóa ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (dr == DialogResult.Yes)
            {
                int index = gridView_DetailService.FocusedRowHandle;
                var item = gridView_DetailService.GetRow(index) as DichVuViewModel;
                lstDichVuViewModel.Remove(item);
                var temp = db.PhuThuDichVus.SingleOrDefault(t => t.MaPhuThu == item.MaPhuThu);
                if (temp != null)
                {
                    //update lai tam ung
                    var tamUng = db.TamUngs.SingleOrDefault(t => t.MaDichVu == temp.MaDichVu && t.MaPhong == temp.MaPhong);
                    if (tamUng != null)
                        tamUng.SoLuongTamUng += temp.SoLuong;
                    db.PhuThuDichVus.DeleteOnSubmit(temp);
                    db.SubmitChanges();
                }

                // recalculate TongTien
                int index1 = gridView_ListRoom.FocusedRowHandle;
                var item1 = gridView_ListRoom.GetRow(index1) as DangKyPhongViewModel;
                var tienTe1 = db.TienTes.SingleOrDefault(t => t.MaDonVi == item1.GiaPhong_DonViTienTe);
                tongTienVND = 0;
                tongTienVND += CommonFunction.ParseDouble(item1.strThanhTienVND);
                foreach (var vmDichVu in lstDichVuViewModel)
                {
                    tongTienVND += vmDichVu.ThanhTienVND;
                }

                tongTienVND -= (CommonFunction.ParseDouble(item1.strTienCoc) * tienTe1.TyGiaThamKhao.Value);
                txtTongTien.Text = CommonFunction.AddSeparator(tongTienVND);
                cmbTienTe.SelectedValue = "VND";

                gridDetailService.DataSource = null;
                gridDetailService.DataSource = lstDichVuViewModel;
            }
        }        

    }
}